//
//  ArtViewController.m
//  开心一刻
//
//  Created by lanou on 16/4/6.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "ArtViewController.h"
#import "Requestmanager.h"
#import "OYZartViewModel.h"
#import "OYZartTableViewCell.h"
#import "OYZxiangqingViewController.h"
#import "OYZtableviewheard.h"
#import "UIImageView+WebCache.h"
@interface SDWebImageManager  (cache)


- (BOOL)memoryCachedImageExistsForURL:(NSURL *)url;

@end

@implementation SDWebImageManager (cache)

- (BOOL)memoryCachedImageExistsForURL:(NSURL *)url {
    NSString *key = [self cacheKeyForURL:url];
    return ([self.imageCache imageFromMemoryCacheForKey:key] != nil) ?  YES : NO;
}

@end

@interface ArtViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableview1;
@property(nonatomic,strong)NSMutableArray *modelarray1;
@property(nonatomic,strong)NSMutableArray *modelarray2;
@property(nonatomic,strong)OYZtableviewheard *tableviewheard1;

@end

@implementation ArtViewController

- (void)wangluoqingqiu
{
    [Requestmanager requestWithUrl:@"http://api.artgoer.cn:8084/artgoer/api/v1/user/0/v3/topic/recommendHome?pageIndex=1&token=df68e038-143e-41cb-b554-456f78f184fc" requesType:requestTypeGET parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        _modelarray1 = [OYZartViewModel jiexiArtview:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableview1 reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"网络错误");
    }];
    [Requestmanager requestWithUrl:@"http://api.artgoer.cn:8084/artgoer/api/v1/user/179872/topicsmarketing?token=3c4400f3-eea4-42f0-9a5c-7cbedc6bc85d" requesType:requestTypeGET parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        _modelarray2 = [OYZartViewModel jiexiArtview:dic];
        [_tableviewheard1 setimage:_modelarray2];
        _tableview1.tableHeaderView = _tableviewheard1;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableview1 reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"网络错误");
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [self wangluoqingqiu];
    _tableview1 = [[UITableView alloc] initWithFrame:CGRectMake(                                 0, 0, KedWidth, KedHeight-64) style:UITableViewStylePlain];
    _tableview1.delegate = self;
    _tableview1.dataSource = self;
    [self.tableview1 registerClass:[OYZartTableViewCell class] forCellReuseIdentifier:@"yishucell"];
    _tableviewheard1 = [[OYZtableviewheard alloc] initWithFrame:CGRectMake(0, 0, KedWidth, KedWidth *900/1500+35)];
    [self.view addSubview:_tableview1];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelarray1.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OYZartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"yishucell" forIndexPath:indexPath];
    OYZartViewModel *model = _modelarray1[indexPath.row];
    [cell fuzhimodel:model];
    [self donghuaxuanzhua:indexPath.row Cell:cell];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OYZxiangqingViewController *xiangqingVc = [[OYZxiangqingViewController alloc] init];
    OYZartViewModel *model = _modelarray1[indexPath.row];
    xiangqingVc.weburl = model.marketingDesc;
//    xiangqingVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:xiangqingVc animated:YES];
//    self.hidesBottomBarWhenPushed = NO;
}
////添加每个cell出现时的3D动画
////-(void)tableView:(UITableView *)tableView willDisplayCell:(EveryDayTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    OYZartViewModel *model = _modelarray1[indexPath.row];
//    if (![[SDWebImageManager sharedManager] memoryCachedImageExistsForURL:[NSURL URLWithString:model.topicPic]]) {
//        
//        CATransform3D rotation;//3D旋转
//        
//        rotation = CATransform3DMakeTranslation(0 ,50 ,20);
//        //        rotation = CATransform3DMakeRotation( M_PI_4 , 0.0, 0.7, 0.4);
//        //逆时针旋转
//        
//        rotation = CATransform3DScale(rotation, 0.5, .8, 1);
//        
//        rotation.m34 = 1.0/ -600;
//        
//        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
//        cell.layer.shadowOffset = CGSizeMake(10, 10);
//        cell.alpha = 0;
//        
//        cell.layer.transform = rotation;
//        
//        [UIView beginAnimations:@"rotation" context:NULL];
//        //旋转时间
//        [UIView setAnimationDuration:1];
//        cell.layer.transform = CATransform3DIdentity;
//        cell.alpha = 1;
//        cell.layer.shadowOffset = CGSizeMake(0, 0);
//        [UIView commitAnimations];
//    }
//    
////    [cell cellOffset];
////    cell.model = model;
//}

- (void)donghuaxuanzhua:(NSInteger)row Cell:(OYZartTableViewCell *)cell
{
    OYZartViewModel *model = _modelarray1[row];
    if (![[SDWebImageManager sharedManager] memoryCachedImageExistsForURL:[NSURL URLWithString:model.topicPic]]) {
        
        CATransform3D rotation;//3D旋转
        
        rotation = CATransform3DMakeTranslation(0 ,50 ,20);
        //        rotation = CATransform3DMakeRotation( M_PI_4 , 0.0, 0.7, 0.4);
        //逆时针旋转
        
        rotation = CATransform3DScale(rotation, 0.5, .8, 1);
        
        rotation.m34 = 1.0/ -600;
        
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.layer.shadowOffset = CGSizeMake(10, 10);
        cell.alpha = 0;
        
        cell.layer.transform = rotation;
        
        [UIView beginAnimations:@"rotation" context:NULL];
        //旋转时间
        [UIView setAnimationDuration:1];
        cell.layer.transform = CATransform3DIdentity;
        cell.alpha = 1;
        cell.layer.shadowOffset = CGSizeMake(0, 0);
        [UIView commitAnimations];
    }
    
    //    [cell cellOffset];
    //    cell.model = model;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return KedWidth*450/750;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
